﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using DAO;

namespace GUI
{
    public partial class frmSetting : Form
    {
        public frmSetting()
        {
            InitializeComponent();
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void frmSetting_Load(object sender, EventArgs e)
        {
            txtServer.Focus();
            filepath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            filepath += "\\QLNhaHang\\setting.txt";
        }
        string filepath;
        public FormDangNhap frmCha;
        public FormQuanLy frmCha2;
        private void bntXong_Click(object sender, EventArgs e)
        {
            if (txtServer.Text.Trim() == "")
            {
                lblErServer.Text = "(*)";
                txtServer.Focus();
                return;
            }
            if (txtDB.Text.Trim() == "")
            {
                lblErDB.Text = "(*)";
                txtDB.Focus();
                return;
            }
                   
            string[] txt = new string[4];
            txt[0] = txtServer.Text.Trim();
            txt[1] = txtDB.Text.Trim();
            if (rdiYES.Checked)
            {
                txt[2] = txtUID.Text.Trim();
                txt[3] = txtPwd.Text.Trim();
            }

            //DataProvider da = new DataProvider();
            DataProvider.NamePC = txt[0];
            DataProvider.DataBase = txt[1];
            DataProvider.UID = txt[2];
            DataProvider.Pwd = txt[3];

            if (!DataProvider.isConnected())
            {
                lblError.Text = "Không thể kết nối CSDL! Thử lại!";
                DataProvider.NamePC = null;
                DataProvider.DataBase = null;
                DataProvider.UID = null;
                DataProvider.Pwd = null;
                return;
            }

            frmCha.txt = txt;
            this.Close();
        }

        private void bntLamLai_Click(object sender, EventArgs e)
        {
            txtDB.Text = "";
            txtServer.Text = "";
            txtPwd.Text = "";
            txtUID.Text = "";
            rdiNO.Checked = true;
            txtServer.Focus();
        }

        private void bntThoat_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void rdiYES_CheckedChanged(object sender, EventArgs e)
        {
            if (rdiYES.Checked)
            {
                txtUID.Enabled = true;
                txtPwd.Enabled = true;
            }
        }

        private void rdiNO_CheckedChanged(object sender, EventArgs e)
        {
            if (rdiNO.Checked)
            {
                txtUID.Enabled = false;
                txtPwd.Enabled = false;
            }
        }

        private void txtServer_TextChanged(object sender, EventArgs e)
        {
            if (txtServer.TextLength > 0)
            {
                lblErServer.Text = "";
                lblError.Text = "";
            }
        }

        private void txtDB_TextChanged(object sender, EventArgs e)
        {
            if (txtDB.TextLength > 0)
            {
                lblErDB.Text = "";
                lblError.Text = "";
            }
        }
    }
}
